package com.minivision.sdktestdemo.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.ui.custom.DiskLrcLoader;
import com.minivision.sdktestdemo.ui.custom.LrcProviderImpl;
import com.minivision.sdktestdemo.ui.custom.LrcScrollView;
import com.minivision.sdktestdemo.ui.custom.LrcService;
import com.minivision.sdktestdemo.ui.custom.NetLrcLoader;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MusicLrcFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MusicLrcFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MusicLrcFragment extends Fragment implements MusicPlayActivity.LrcListener, View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private MusicPicFragment.OnFragmentInteractionListener mListener;

    public MusicLrcFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MusicLrcFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MusicLrcFragment newInstance(String param1, String param2) {
        MusicLrcFragment fragment = new MusicLrcFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        isInit = true;
    }

    @Bind(R.id.lrcScrollView)
    LrcScrollView mLrcView;

    private LrcProviderImpl mLrcProvider;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_music_lrc, container, false);
        ButterKnife.bind(this, view);
        initData();
        initView();
        return view;
    }

    private void initView() {
        mLrcView.setLrcView(mLrcProvider);
        mLrcView.setOnClickListener(this);
        if (!mMusicName.isEmpty()) {
            onMusicChanged(mMusicName);
        }
    }

    private void initData() {
        mLrcProvider = new LrcProviderImpl(getContext());
        mLrcProvider.setLrcLoader(new DiskLrcLoader(getContext()).setLrcLoader(new NetLrcLoader(getContext(), LrcService.BASE_URL)));
    }


    @Override
    public void onDestroyView() {
        ButterKnife.unbind(this);
        super.onDestroyView();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mListener = (MusicPicFragment.OnFragmentInteractionListener) context;
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onLrcChanged(long time) {
        mLrcView.displayPresentLrc(time);
    }

    private String mMusicName = "";
    private boolean isInit;
    @Override
    public void onMusicChanged(final String name) {
        if (!mMusicName.equals(name) || isInit) {
            isInit = false;
            mMusicName = name;
            if (mLrcProvider != null) {
                mLrcView.clearLrcList(getString(R.string.lrcWait));
                mLrcProvider.queryNewLrc(name, new Action1<String>() {
                    @Override
                    public void call(String s) {
                        if (name.equals(mMusicName)) {
                            if (s.isEmpty()) {
                                mLrcView.clearLrcList("没有找到相关歌词");
                            } else {
                                mLrcView.setLrcView(mLrcProvider);
                            }
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onClick(View v) {
        mListener.changeFragment();
    }
}
